<!DOCTYPE html><html lang="en" data-beasties-container><head>
  <meta charset="UTF-8">
  <title>Indexes</title>
  <meta name="description" content="Djongo is a smarter approach to database querying. It maps python objects to MongoDB documents.
         It is popularly referred to as an Object Document Mapper or ODM.
         It is an extension to the traditional Django object relational modeling framework.
         Use Django Admin to directly add and modify documents stored in MongoDB.
         Use other contrib modules such as Auth and Sessions without any changes.">
  <meta name="keywords" content="Django, MongoDB, Django MongoDB connector, Object document mapper">
  <meta property="og:type" content="website">
  <meta property="og:locale" content="en_US">
  <meta property="og:site_name" content="Djongomapper">
  <meta property="og:title" content="Djongo">
  <meta property="og:url" content="https://www.djongomapper.com/">
  <meta property="og:description" content="Djongo is a smarter approach to database querying. It maps python objects to MongoDB documents. It is popularly referred to as an Object Document Mapper or ODM. It is an extension to the traditional Django object relational modeling framework. Use Django Admin to directly add and modify documents stored in MongoDB. Use other contrib modules such as Auth and Sessions without any changes.">
  <link rel="canonical" href="https://www.djongomapper.com/">
  <script type="application/ld+json">
    {
      "@context": "https://schema.org",
      "@type": "Organization",
      "name": "Doableware",
      "url": "https://www.djongomapper.com/",
      "logo": "/assets/images/djongo-symbol-small.png"
    }
  </script>
  <meta name="google-site-verification" content="tFNu7Nc9hzKF9v7f6kYaoMbBCNyGREchcKMVdxJuyJo">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="preload" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5/css/all.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
  <noscript>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5/css/all.min.css">
  </noscript>
  <link rel="stylesheet" type="text/css" media="screen" href="/assets/css/styles.css">
  <link rel="shortcut icon" href="/assets/images/favicon.ico">
  <script>
    if (window.location.hostname != 'localhost') {
      (function (w, d, s, l, i) {
        w[l] = w[l] || [];
        w[l].push({
                    'gtm.start':
                      new Date().getTime(), event: 'gtm.js'
                  });
        var f = d.getElementsByTagName(s)[0],
          j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : '';
        j.async = true;
        j.src =
          'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
        f.parentNode.insertBefore(j, f);
      })(window, document, 'script', 'dataLayer', 'GTM-T72KG6N');
    }
  </script>
  <base href="/">
<link rel="stylesheet" href="styles-JF5ME2K6.css" media="print" onload="this.media='all'"><noscript><link rel="stylesheet" href="styles-JF5ME2K6.css"></noscript><style ng-app-id="ng">.cookie-notice[_ngcontent-ng-c2830357685]{height:0}.open[_ngcontent-ng-c2830357685]{height:auto}.close[_ngcontent-ng-c2830357685]{height:0;transition:height .3s ease-out}.mast[_ngcontent-ng-c2830357685]{position:sticky;top:0;padding:.5em 0;z-index:10;background:#fff;border-bottom:1px solid rgb(241.7553,242.60496,243.2547)}@media (max-width: 90rem){.mast[_ngcontent-ng-c2830357685]{padding:.5em 1em}}.mast[_ngcontent-ng-c2830357685] > div[_ngcontent-ng-c2830357685]{margin:0 auto;max-width:80rem;display:flex;flex-direction:row;justify-content:space-between}.mast[_ngcontent-ng-c2830357685]   a[_ngcontent-ng-c2830357685]{display:block;color:#62696e;text-decoration:none}.mast[_ngcontent-ng-c2830357685]   .mast-logo[_ngcontent-ng-c2830357685]{display:flex;gap:var(--space-xs);align-items:center}.mast[_ngcontent-ng-c2830357685]   .mast-logo[_ngcontent-ng-c2830357685]   img[_ngcontent-ng-c2830357685]{max-height:2em}.mast[_ngcontent-ng-c2830357685]   .mast-nav[_ngcontent-ng-c2830357685]   ul[_ngcontent-ng-c2830357685]{display:flex;align-items:center;gap:var(--space-md)}.mast[_ngcontent-ng-c2830357685]   .mast-nav[_ngcontent-ng-c2830357685]   ul.media-small[_ngcontent-ng-c2830357685]{padding-top:var(--space-sm);padding-bottom:var(--space-sm);flex-direction:column;align-items:start;gap:var(--space-xs)}.mast[_ngcontent-ng-c2830357685]   .mast-nav[_ngcontent-ng-c2830357685]   .nav-link[_ngcontent-ng-c2830357685]{position:relative}.mast[_ngcontent-ng-c2830357685]   .mast-nav[_ngcontent-ng-c2830357685]   .nav-link[_ngcontent-ng-c2830357685]:hover:after{width:100%}.mast[_ngcontent-ng-c2830357685]   .mast-nav[_ngcontent-ng-c2830357685]   .nav-link[_ngcontent-ng-c2830357685]:after{content:"";position:absolute;left:50%;bottom:0;transform:translate(-50%);width:0;height:4px;background-color:#62696e;transition:width .2s ease-in-out}.mast[_ngcontent-ng-c2830357685]   .mast-nav[_ngcontent-ng-c2830357685]   .btn-nav[_ngcontent-ng-c2830357685]{padding:.5em 1em;font-size:var(--font-sm);font-weight:700;text-align:center;border-width:0;border-radius:.1875em;color:#fff}.burger[_ngcontent-ng-c2830357685]{display:flex;flex-direction:column;gap:4px}.burger[_ngcontent-ng-c2830357685] > div[_ngcontent-ng-c2830357685]{height:4px;width:var(--space-md);background-color:#62696e;border-radius:4px}.burger[_ngcontent-ng-c2830357685]:hover{cursor:pointer}</style><style ng-app-id="ng">.media-small[_ngcontent-ng-c40719357]{position:fixed;top:3rem;left:0;padding:0 var(--space-sm);background:#fff;height:100vh;border-right:1px solid rgb(241.7553,242.60496,243.2547);z-index:1}.cross[_ngcontent-ng-c40719357]{margin:var(--space-2xs) 0;aspect-ratio:1;width:var(--font-md)}.cross[_ngcontent-ng-c40719357]:hover{cursor:pointer}.cross[_ngcontent-ng-c40719357] > div[_ngcontent-ng-c40719357]{background:#000;height:1px;width:var(--font-md)}.cross[_ngcontent-ng-c40719357] > div[_ngcontent-ng-c40719357]:nth-child(1){transform:translateY(calc(var(--font-md) * .5)) rotate(-45deg)}.cross[_ngcontent-ng-c40719357] > div[_ngcontent-ng-c40719357]:nth-child(2){transform:translateY(calc(var(--font-md) * .5 - 1px)) rotate(45deg)}.open[_ngcontent-ng-c40719357]{transition:transform .4s ease-in;transform:translate(0)}.close[_ngcontent-ng-c40719357]{transform:translate(-100%);padding:0;overflow:hidden;transition:transform .3s ease-out}</style><style ng-app-id="ng">.body[_ngcontent-ng-c2889142148]{display:grid;grid-template-columns:minmax(max-content,calc(16rem - 1em)) minmax(16rem,80rem) minmax(min-content,calc(16rem - 1em));grid-template-rows:repeat(2,auto);justify-content:center;column-gap:1em;padding-left:1em;padding-right:1em}.page-content[_ngcontent-ng-c2889142148]{grid-row:1/-1}.aside-left[_ngcontent-ng-c2889142148], .aside-right[_ngcontent-ng-c2889142148]{grid-row:1}@media (max-width: 80rem){.body[_ngcontent-ng-c2889142148]{grid-template-columns:max-content minmax(24rem,80rem);grid-template-rows:repeat(3,auto)}.aside-left[_ngcontent-ng-c2889142148]{grid-column:1;grid-row:1/3}.page-content[_ngcontent-ng-c2889142148]{grid-column:2;grid-row:2/-1}.aside-right[_ngcontent-ng-c2889142148]{grid-column:2;grid-row:1/2}}@media (max-width: 48rem){.body[_ngcontent-ng-c2889142148]{grid-template-rows:repeat(2,auto)}.aside-left[_ngcontent-ng-c2889142148]{grid-row:1/-1}.aside-right[_ngcontent-ng-c2889142148]{grid-row:1}.page-content[_ngcontent-ng-c2889142148]{grid-row:2}}.nav[_ngcontent-ng-c2889142148]{padding-top:1em;padding-bottom:1em;min-width:min-content;max-width:max-content;font-size:var(--font-sm)}@media (max-width: 80rem){.nav[_ngcontent-ng-c2889142148]{font-size:var(--font-base)}}.nav[_ngcontent-ng-c2889142148]   a[_ngcontent-ng-c2889142148]{text-decoration:none}.nav-sticky[_ngcontent-ng-c2889142148]{position:sticky;top:3em;overflow:auto;max-height:calc(100vh - 3em)}.nav-sticky.aside-left[_ngcontent-ng-c2889142148]{z-index:1}@media (max-width: 80rem){.nav-sticky.aside-right[_ngcontent-ng-c2889142148]{position:static;max-height:inherit}}.nav-side[_ngcontent-ng-c2889142148]{line-height:1.58}.nav-side[_ngcontent-ng-c2889142148]   h3[_ngcontent-ng-c2889142148]{font-size:1em;opacity:.7;text-transform:uppercase;border-bottom:1px solid rgb(241.7553,242.60496,243.2547);margin:.5em 0;padding:.25em 0}.nav-side[_ngcontent-ng-c2889142148]   a[_ngcontent-ng-c2889142148]{color:inherit;opacity:.7}.nav-side[_ngcontent-ng-c2889142148]   .active[_ngcontent-ng-c2889142148]{font-weight:700;opacity:1}.nav-side[_ngcontent-ng-c2889142148]   a[_ngcontent-ng-c2889142148]:hover{opacity:1}.nav-toc[_ngcontent-ng-c2889142148]   h3[_ngcontent-ng-c2889142148]{font-size:var(--font-base);font-weight:700;color:#fff;background:#62696e;border-top-left-radius:.375em;border-top-right-radius:.375em;padding:var(--space-2xs) var(--space-xs);margin:0}.nav-toc[_ngcontent-ng-c2889142148]   a[_ngcontent-ng-c2889142148]{display:block;padding:var(--space-2xs) var(--space-xs);color:#646769;font-weight:700;line-height:1.5;border-bottom:1px solid rgb(241.7553,242.60496,243.2547);border-left:1px solid rgb(241.7553,242.60496,243.2547);border-right:1px solid rgb(241.7553,242.60496,243.2547)}.nav-toc[_ngcontent-ng-c2889142148]   a[_ngcontent-ng-c2889142148]:hover{text-decoration:underline}.nav-toc[_ngcontent-ng-c2889142148]   li[_ngcontent-ng-c2889142148]   li[_ngcontent-ng-c2889142148] > a[_ngcontent-ng-c2889142148]{font-weight:400;padding-left:1em}.nav-toc[_ngcontent-ng-c2889142148]   .active[_ngcontent-ng-c2889142148]{background-color:#e2e4e5}.sidebar[_ngcontent-ng-c2889142148]{opacity:1}.sidebar[_ngcontent-ng-c2889142148]   li[_ngcontent-ng-c2889142148]{font-size:.7em}.page-content[_ngcontent-ng-c2889142148]{margin-top:1em}</style></head>
<body><!--nghm--><script type="text/javascript" id="ng-event-dispatch-contract">(()=>{function p(t,n,r,o,e,i,f,m){return{eventType:t,event:n,targetElement:r,eic:o,timeStamp:e,eia:i,eirp:f,eiack:m}}function u(t){let n=[],r=e=>{n.push(e)};return{c:t,q:n,et:[],etc:[],d:r,h:e=>{r(p(e.type,e,e.target,t,Date.now()))}}}function s(t,n,r){for(let o=0;o<n.length;o++){let e=n[o];(r?t.etc:t.et).push(e),t.c.addEventListener(e,t.h,r)}}function c(t,n,r,o,e=window){let i=u(t);e._ejsas||(e._ejsas={}),e._ejsas[n]=i,s(i,r),s(i,o,!0)}window.__jsaction_bootstrap=c;})();
</script><script>window.__jsaction_bootstrap(document.body,"ng",["click"],[]);</script>
<app-root ng-version="20.1.4" _nghost-ng-c2830357685 ngh="3" ng-server-context="ssg"><div _ngcontent-ng-c2830357685 class="page-splash"><div _ngcontent-ng-c2830357685 class="mast"><div _ngcontent-ng-c2830357685><div _ngcontent-ng-c2830357685 class="mast-logo"><a _ngcontent-ng-c2830357685 href="/"><img _ngcontent-ng-c2830357685 src="/assets/images/djongo-symbol-small.png" alt="Djongo"></a><!----></div><div _ngcontent-ng-c2830357685 class="mast-nav"><app-nav _ngcontent-ng-c2830357685 _nghost-ng-c40719357 ngh="0"><!----><!----><!----></app-nav></div></div></div><div _ngcontent-ng-c2830357685 class="initial-content"><router-outlet _ngcontent-ng-c2830357685></router-outlet><ng-component _nghost-ng-c2889142148 ngh="2"><div _ngcontent-ng-c2889142148 class="body"><aside _ngcontent-ng-c2889142148 class="nav-sticky aside-left"><app-nav _ngcontent-ng-c2889142148 _nghost-ng-c40719357 ngh="0"><!----><!----><!----></app-nav></aside><div _ngcontent-ng-c2889142148 class="page-content"><header _ngcontent-ng-c2889142148><h1 _ngcontent-ng-c2889142148 class="title-docs">Indexes</h1></header><router-outlet _ngcontent-ng-c2889142148></router-outlet><ng-component ngh="1"><h2 id="compound-index">Compound Index</h2><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="k">class</span> <span class="nc">CompoundIndex</span><span class="p">(</span>
    <span class="n">fields</span><span class="o">=</span><span class="p">(),</span>
    <span class="n">name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">unique</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">background</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">partialFilterExpression</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">sparse</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">collation</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><h3 id="arguments">Arguments</h3><table><thead><tr><th>Argument</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><code>fields</code></td><td><code>Tuple</code> or <code>List</code></td><td>A tuple of strings containing the fields to be indexed. Adding a '-' before the field name, builds the index in the <a href="https://docs.djangoproject.com/en/dev/ref/models/indexes/#django.db.models.Index.fields">descending order</a>.</td></tr><tr><td><code>name</code></td><td><code>string</code></td><td>The index name, if not provided will be autogenerated by MongoDB</td></tr><tr><td><code>unique</code></td><td><code>boolean</code></td><td>Used to create <a href="https://docs.mongodb.com/manual/core/index-unique/">unique indexes</a></td></tr><tr><td><code>background</code></td><td><code>boolean</code></td><td>Create the index in the background.</td></tr><tr><td><code>partialFilterExpression</code></td><td><code>Q</code></td><td>Used to create <a href="https://docs.mongodb.com/manual/core/index-partial/">partial indexes</a>. Similar to Django <a href="https://docs.djangoproject.com/en/dev/ref/models/indexes/#condition">Index condition</a>. Takes an instance of <a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.Q">Q</a> to build the filter expression.</td></tr><tr><td><code>sparse</code></td><td><code>boolean</code></td><td>Used to create <a href="https://docs.mongodb.com/manual/core/index-sparse/">sparse indexes</a></td></tr><tr><td><code>collation</code></td><td><code>Collation</code></td><td>Used to specify the <a href="https://docs.mongodb.com/manual/reference/collation/">collation</a>. Takes an instance of <a href="https://api.mongodb.com/python/current/api/pymongo/collation.html">collation</a></td></tr></tbody></table><h3 id="example">Example</h3><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">djongo.models.indexes</span> <span class="kn">import</span> <span class="n">CompoundIndex</span>
<span class="kn">from</span> <span class="nn">djongo</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Blog</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">tagline</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">indexes</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">CompoundIndex</span><span class="p">(</span><span class="n">fields</span><span class="o">=</span><span class="p">[</span><span class="s1">'name'</span><span class="p">,</span> <span class="s1">'-tagline'</span><span class="p">])</span>
        <span class="p">]</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><p>Generates the pymongo command:</p><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="n">db</span><span class="o">.</span><span class="n">blog</span><span class="o">.</span><span class="n">create_index</span><span class="p">(</span> <span class="p">{</span> <span class="s1">'name'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">'tagline'</span><span class="p">:</span> <span class="o">-</span><span class="mi">1</span> <span class="p">}</span> <span class="p">)</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><h2 id="text-index">Text Index</h2><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="k">class</span> <span class="nc">TextIndex</span><span class="p">(</span>
    <span class="n">fields</span><span class="o">=</span><span class="p">(),</span>
    <span class="n">name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">unique</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">background</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">partialFilterExpression</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">weights</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> 
    <span class="n">default_language</span><span class="o">=</span><span class="s1">'english'</span><span class="p">,</span> 
    <span class="n">language_override</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> 
    <span class="n">textIndexVersion</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><h3 id="example_1">Example</h3><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">djongo.models.indexes</span> <span class="kn">import</span> <span class="n">TextIndex</span>
<span class="kn">from</span> <span class="nn">djongo</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Blog</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">tagline</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">indexes</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">TextIndex</span><span class="p">(</span><span class="n">fields</span><span class="o">=</span><span class="p">[</span><span class="s1">'name'</span><span class="p">,</span> <span class="s1">'tagline'</span><span class="p">])</span>
        <span class="p">]</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><p>Generates the pymongo command:</p><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="n">db</span><span class="o">.</span><span class="n">blog</span><span class="o">.</span><span class="n">create_index</span><span class="p">(</span> <span class="p">{</span> <span class="s1">'name'</span><span class="p">:</span> <span class="s1">'text'</span><span class="p">,</span> <span class="s1">'tagline'</span><span class="p">:</span> <span class="s1">'text'</span> <span class="p">}</span> <span class="p">)</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><h2 id="wildcard-index">Wildcard Index</h2><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="k">class</span> <span class="nc">WildcardIndex</span><span class="p">(</span>
    <span class="n">fields</span><span class="o">=</span><span class="p">(),</span>
    <span class="n">name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">unique</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">background</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">partialFilterExpression</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">sparse</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">collation</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">wildcardProjection</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><h3 id="example_2">Example</h3><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">djongo.models.indexes</span> <span class="kn">import</span> <span class="n">WildcardIndex</span>
<span class="kn">from</span> <span class="nn">djongo</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Blog</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">tagline</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">abstract</span> <span class="o">=</span> <span class="kc">True</span>

<span class="k">class</span> <span class="nc">Entry</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">blog</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">EmbeddedField</span><span class="p">(</span>
        <span class="n">model_container</span><span class="o">=</span><span class="n">Blog</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">indexes</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">WildcardIndex</span><span class="p">(</span><span class="n">fields</span><span class="o">=</span><span class="p">[</span><span class="s1">'blog'</span><span class="p">])</span>
        <span class="p">]</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><p>Generates the pymongo command:</p><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="n">db</span><span class="o">.</span><span class="n">entry</span><span class="o">.</span><span class="n">create_index</span><span class="p">(</span> <span class="p">{</span> <span class="s1">'blog.$**'</span><span class="p">:</span> <span class="mi">1</span> <span class="p">}</span> <span class="p">)</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><h2 id="2dsphere-index">2dsphere Index</h2><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="k">class</span> <span class="nc">TwoDSphereIndex</span><span class="p">(</span>
    <span class="n">fields</span><span class="o">=</span><span class="p">(),</span>
    <span class="n">name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">unique</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">background</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">partialFilterExpression</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">sparse</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">collation</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="nb">min</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="nb">max</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><h3 id="example_3">Example</h3><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">djongo.models.indexes</span> <span class="kn">import</span> <span class="n">TwoDSphereIndex</span>
<span class="kn">from</span> <span class="nn">djongo</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Location</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="nb">type</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">coordinates</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ArrayField</span><span class="p">()</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">abstract</span> <span class="o">=</span> <span class="kc">True</span>

<span class="k">class</span> <span class="nc">Entry</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">loc</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">EmbeddedField</span><span class="p">(</span>
        <span class="n">model_container</span><span class="o">=</span><span class="n">Location</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">indexes</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">TwoDSphereIndex</span><span class="p">(</span><span class="n">fields</span><span class="o">=</span><span class="p">[</span><span class="s1">'loc'</span><span class="p">])</span>
        <span class="p">]</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><p>Generates the pymongo command:</p><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="n">db</span><span class="o">.</span><span class="n">entry</span><span class="o">.</span><span class="n">create_index</span><span class="p">(</span> <span class="p">{</span> <span class="s1">'loc'</span> <span class="p">:</span> <span class="s1">'2dsphere'</span> <span class="p">}</span> <span class="p">)</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><h2 id="hashed-index">Hashed Index</h2><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="k">class</span> <span class="nc">HashedIndex</span><span class="p">(</span>
    <span class="n">fields</span><span class="o">=</span><span class="p">(),</span>
    <span class="n">name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">unique</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">background</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">partialFilterExpression</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">sparse</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">collation</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><h3 id="example_4">Example</h3><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">djongo.models.indexes</span> <span class="kn">import</span> <span class="n">HashedIndex</span>
<span class="kn">from</span> <span class="nn">djongo</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Blog</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">tagline</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">indexes</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">HashedIndex</span><span class="p">(</span><span class="n">fields</span><span class="o">=</span><span class="p">[</span><span class="s1">'name'</span><span class="p">])</span>
        <span class="p">]</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><p>Generates the pymongo command:</p><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="n">db</span><span class="o">.</span><span class="n">blog</span><span class="o">.</span><span class="n">create_index</span><span class="p">(</span> <span class="p">{</span> <span class="s1">'name'</span><span class="p">:</span> <span class="s1">'hashed'</span> <span class="p">}</span> <span class="p">)</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><h2 id="ttl-index">TTL Index</h2><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="k">class</span> <span class="nc">TTLIndex</span><span class="p">(</span>
    <span class="n">field</span><span class="o">=</span><span class="p">,</span>
    <span class="n">name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">unique</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">background</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">partialFilterExpression</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">sparse</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">collation</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">expireAfterSeconds</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><h3 id="example_5">Example</h3><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">djongo.models.indexes</span> <span class="kn">import</span> <span class="n">TTLIndex</span>
<span class="kn">from</span> <span class="nn">djongo</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Blog</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">pub_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">()</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">indexes</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">TTLIndex</span><span class="p">(</span><span class="n">field</span><span class="o">=</span><span class="s1">'pub_date'</span><span class="p">,</span> <span class="n">expireAfterSeconds</span><span class="o">=</span><span class="mi">3600</span><span class="p">)</span>
        <span class="p">]</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div><p>Generates the pymongo command:</p><div appcodecopy class="highlighter-rouge highlight codehilite"><pre><span></span><code><span class="n">db</span><span class="o">.</span><span class="n">blog</span><span class="o">.</span><span class="n">create_index</span><span class="p">(</span> <span class="p">{</span> <span class="s1">'pub_date'</span><span class="p">:</span> <span class="mi">1</span> <span class="p">},</span> <span class="n">expireAfterSeconds</span><span class="o">=</span><span class="mi">3600</span> <span class="p">)</span>
</code></pre><img alt="copy code" src="/assets/images/copy.svg" class="docs-copy"></div></ng-component><!----></div><aside _ngcontent-ng-c2889142148 class="nav-sticky aside-right"><nav _ngcontent-ng-c2889142148 class="nav nav-toc"><h3 _ngcontent-ng-c2889142148><i _ngcontent-ng-c2889142148 class="fas fa-file-alt"></i> On This Page</h3><ul _ngcontent-ng-c2889142148><!----></ul></nav></aside></div></ng-component><!----></div><div _ngcontent-ng-c2830357685 id="footer" class="page-footer"><footer _ngcontent-ng-c2830357685><aside _ngcontent-ng-c2830357685 class="l-fixed-notice cookie-notice open"><p _ngcontent-ng-c2830357685>We use cookies to enhance user experience. You consent to their usage by browsing this site. <button _ngcontent-ng-c2830357685 id="cookie-accept" class="btn btn-green" jsaction="click:;">Accept</button></p></aside><div _ngcontent-ng-c2830357685 class="page-footer-copyright">© 2025 Doableware AB.</div></footer></div></div></app-root>
<script>
  window.ga = function () {
    ga.q.push(arguments)
  };
  ga.q = [];
  ga.l = +new Date;
  ga('create', 'UA-75159067-1', 'auto');
  ga('set', 'anonymizeIp', false);
  ga('send', 'pageview')
</script>
<script src="https://www.google-analytics.com/analytics.js" async></script>
<link rel="modulepreload" href="chunk-GTGB5SJ3.js"><link rel="modulepreload" href="chunk-HEUFHRRO.js"><link rel="modulepreload" href="chunk-RYXYKL7J.js"><link rel="modulepreload" href="chunk-YECZU7CY.js"><link rel="modulepreload" href="chunk-UZ35QZ23.js"><link rel="modulepreload" href="chunk-FDEVB4XX.js"><script src="polyfills-BUUDEW7V.js" type="module"></script><script src="main-TEK63YGN.js" type="module"></script>
<link rel="modulepreload" href="chunk-TTHFXBLJ.js">
<link rel="modulepreload" href="chunk-VWVSK2JB.js">

<script id="ng-state" type="application/json">{"__nghData__":[{"t":{"0":"t1","2":"t2","3":"t3"},"c":{"0":[],"2":[],"3":[]}},{},{"d":[3,4,6],"t":{"6":"t5","20":"t6"},"c":{"12":[{"i":"c3389219281","r":1}],"20":[]}},{"t":{"6":"t0","12":"t4"},"c":{"6":[],"14":[{"i":"c2889142148","r":1}]},"d":[9,10,12]}]}</script></body></html>